package com.hdu.careerplan.util;

import org.bson.Document;
import org.springframework.data.mongodb.core.aggregation.AggregationOperation;
import org.springframework.data.mongodb.core.aggregation.AggregationOperationContext;

import java.util.Arrays;

public class MongoDocumentStructureUtil {

    public static AggregationOperation createDateProjectionDay() {
        return new AggregationOperation() {
            @Override
            public Document toDocument(AggregationOperationContext aggregationOperationContext) {
                return new Document("$project", new Document()
                        .append("salary", 1)
                        .append("jobCnt", 1)
                        .append("date", new Document("$concat",
                                Arrays.asList(
                                        new Document("$toString", "$_id.year"),
                                        "-",
                                        new Document("$cond", Arrays.asList(
                                                new Document("$lt", Arrays.asList(
                                                        new Document("$strLenCP", new Document("$toString", "$_id.month")),
                                                        2)),
                                                new Document("$concat", Arrays.asList("0", new Document("$toString", "$_id.month"))),
                                                new Document("$toString", "$_id.month")

                                        )),
                                        "-",
                                        new Document("$cond", Arrays.asList(
                                                new Document("$lt", Arrays.asList(
                                                        new Document("$strLenCP", new Document("$toString", "$_id.day")),
                                                        2)),
                                                new Document("$concat", Arrays.asList("0", new Document("$toString", "$_id.day"))),
                                                new Document("$toString", "$_id.day")
                                        )
                                        ))
                        ))
                );
            }
        };
    }

    public static AggregationOperation createDateProjectionMonth() {
        return new AggregationOperation() {
            @Override
            public Document toDocument(AggregationOperationContext aggregationOperationContext) {
                return new Document("$project", new Document()
                        .append("salary", 1)
                        .append("jobCnt", 1)
                        .append("date", new Document("$concat",
                                Arrays.asList(
                                        new Document("$toString", "$_id.year"),
                                        "-",
                                        new Document("$cond", Arrays.asList(
                                                new Document("$lt", Arrays.asList(
                                                        new Document("$strLenCP", new Document("$toString", "$_id.month")),
                                                        2)),
                                                new Document("$concat", Arrays.asList("0", new Document("$toString", "$_id.month"))),
                                                new Document("$toString", "$_id.month")

                                        )),
                                        "-",
                                        "01")

                        ))
                );
            }
        };
    }

    public static AggregationOperation createDateProjectionYear() {
        return new AggregationOperation() {
            @Override
            public Document toDocument(AggregationOperationContext aggregationOperationContext) {
                return new Document("$project", new Document()
                        .append("salary", 1)
                        .append("jobCnt", 1)
                        .append("date", new Document("$concat",
                                Arrays.asList(
                                        new Document("$toString", "$_id"),
                                        "-",
                                        "01",
                                        "-",
                                        "01"
                                )
                        ))
                );
            }
        };
    }
}
